#Figure A2: The Causal Effects of Policy Costs on Support for Flood Control Dam by Subgroups
library(foreign)
library(ggplot2)
library(dplyr)
library(xlsx)
library(gridExtra)

#Set working directory
setwd("~/Desktop/Dropbox/Disaster policy/02 Empirics/10 Replicationarchive")
d = read.xlsx("figA2_res_diff.xlsx", sheetIndex = 1)
hl = read.xlsx("figA2_res_est.xlsx", sheetIndex = 1)

#Prepare data
#---------------------------
d = rename(d, env = NA., env_pe = y1, env_se = r1,
           aff = NA..1, aff_pe = y1.1, aff_se = r1.1,
           disrisk = NA..2, disrisk_pe = y1.2, disrisk_se = r1.2,
           riskacc = NA..3, riskacc_pe = y1.3,  riskacc_se = r1.3,
           patience = NA..4, patience_pe = y1.4, patience_se = r1.4,
           party = NA..5, party_pe = y1.5,party_se = r1.5)

d = d[c(1:3, 9, 11), ] #extract only data needed

hl = filter(hl, NA. == "2.dam_costs" | NA. == "3.dam_costs")
hl = select(hl, 19:36) 
hl = rename(hl, env = NA..6, env_pe = y1.6, env_se = r1.6,
            aff = NA..7, aff_pe = y1.7, aff_se = r1.7,
            disrisk = NA..8, disrisk_pe = y1.8, disrisk_se = r1.8,
            riskacc = NA..9, riskacc_pe = y1.9, riskacc_se = r1.9,
            patience = NA..10, patience_pe = y1.10, patience_se = r1.10,
            party = NA..11, party_pe = y1.11, party_se = r1.11)

#Environemntalism
env = select(hl, 1:3)
env$groupenv = "Environmentalism: High"
env = select(env, -env)
newrow = c(0,0,"Environmentalism: High")
env = rbind(env,newrow)

env1 = select(d, 1:3)
env1$groupenv = ifelse(env1$env=="2.dam_costs#1.env_income_high" | env1$env=="3.dam_costs#1.env_income_high","Difference","Environmentalism: Low")
env1 = select(env1, -env)

env = rbind(env,env1)
var = as.data.frame(c("Costs: Medium ($2 Million)", "Costs: Low ($8 Million)", "Costs: High ($14 Million)", "Costs: High ($14 Million)", "Costs: Medium ($8 Million)", "Costs: Low ($2 Million)", "diff_costslow", "diff_costshigh"))
names(var) = "var"
env = cbind(var,env)
env$env_pe = as.numeric(env$env_pe)
env$env_se = as.numeric(env$env_se)

#Affectedness
aff = select(hl, 4:6)
aff$groupaff = "Affected"
aff = select(aff, -aff)
newrow = c(0,0,"Affected")
aff = rbind(aff,newrow)

aff1 = select(d, 4:6)
aff1$groupaff = ifelse(aff1$aff=="2.dam_costs#1.paff_high" | aff1$aff=="3.dam_costs#1.paff_high","Difference","Not affected")
aff1 = select(aff1, -aff)

aff = rbind(aff,aff1)
aff$aff_pe = as.numeric(aff$aff_pe)
aff$aff_se = as.numeric(aff$aff_se)

#Disaster risk
disrisk = select(hl, 7:9)
disrisk$groupdisrisk = "Risk Destructive Events: High"
disrisk = select(disrisk, -disrisk)
newrow = c(0,0,"Risk Destructive Events: High")
disrisk = rbind(disrisk,newrow)

disrisk1 = select(d, 7:9)
disrisk1$groupdisrisk = ifelse(disrisk1$disrisk=="2.dam_costs#1.riskdest_high" | disrisk1$disrisk=="3.dam_costs#1.riskdest_high","Difference","Risk Destructive Events: Low")
disrisk1 = select(disrisk1, -disrisk)

disrisk = rbind(disrisk,disrisk1)
disrisk$disrisk_pe = as.numeric(disrisk$disrisk_pe)
disrisk$disrisk_se = as.numeric(disrisk$disrisk_se)

#Risk acceptance 
riskacc = select(hl, 10:12)
riskacc$groupriskacc = "Risk Aversion: Low"
riskacc = select(riskacc, -riskacc)
newrow = c(0,0,"Risk Aversion: Low")
riskacc = rbind(riskacc,newrow)

riskacc1 = select(d, 10:12)
riskacc1$groupriskacc = ifelse(riskacc1$riskacc=="2.dam_costs#1.riskpref_surv_high" | riskacc1$riskacc=="3.dam_costs#1.riskpref_surv_high","Difference","Risk Aversion: High")
riskacc1 = select(riskacc1, -riskacc)

riskacc = rbind(riskacc,riskacc1)
riskacc$riskacc_pe = as.numeric(riskacc$riskacc_pe)
riskacc$riskacc_se = as.numeric(riskacc$riskacc_se)

#Patience
patience = select(hl, 13:15)
patience$grouppatience = "Patience: High"
patience = select(patience, -patience)
newrow = c(0,0,"Patience: High")
patience = rbind(patience,newrow)

patience1 = select(d, 13:15)
patience1$grouppatience = ifelse(patience1$patience=="2.dam_costs#1.timepref_surv_high" | patience1$patience=="3.dam_costs#1.timepref_surv_high","Difference","Patience: Low")
patience1 = select(patience1, -patience)

patience = rbind(patience,patience1)
patience$patience_pe = as.numeric(patience$patience_pe)
patience$patience_se = as.numeric(patience$patience_se)

#Partisanship
party = select(hl, 16:18)
party$groupparty = "Republican"
party = select(party, -party)
newrow = c(0,0,"Republican")
party = rbind(party,newrow)

party1 = select(d, 16:18)
party1$groupparty = ifelse(party1$party=="2.dam_costs#1.RepublicanvsDemo" | party1$party=="3.dam_costs#1.RepublicanvsDemo","Difference","Democrat")
party1 = select(party1, -party)

party = rbind(party1, party)
party$party_pe = as.numeric(party$party_pe)
party$party_se = as.numeric(party$party_se)

ordering_party = data.frame(c(3,1,2,7,8,5,6,4))
names(ordering_party) = "ordering"
party = cbind(party,ordering_party)
party = arrange(party, party$ordering)
party = select(party, -ordering)


#Put all of the above together and transform data
d = cbind(env,aff,disrisk,riskacc,patience,party)

sqrf <- function(x){
  sqrt(x)
}

dsub <- select(d, env_se, aff_se, disrisk_se, riskacc_se, patience_se, party_se)


dse <- lapply(dsub, sqrf)
dse <- as.data.frame(dse)
d = select(d, -env_se, -aff_se, -disrisk_se, -riskacc_se, -patience_se, -party_se)
d <- cbind(d, dse)

ordering = data.frame(c(4,6,2,1,3,5,7,8))
names(ordering) = "ordering"
d = cbind(d,ordering)
d = arrange(d, d$ordering)


#Generating upper and lower bounds and preparing figure
#Environemntalism
d$env_lower <- d$env_pe  - 1.96*d$env_se
d$env_upper <- d$env_pe  + 1.96*d$env_se
order = data.frame(c(0.8,1.2,1.8,2.2,3.8,4.2,3,5))
names(order) = "order"
d = cbind(d,order)
d$order <- d$order * -1 + 6

d$groupenv <- as.factor(d$groupenv)
d$groupenv <- factor(d$groupenv, levels = c(levels(d$groupenv)[3],levels(d$groupenv)[2],levels(d$groupenv)[1]))

#Affectedness
d$aff_lower <- d$aff_pe  - 1.96*d$aff_se
d$aff_upper <- d$aff_pe  + 1.96*d$aff_se

d$groupaff <- as.factor(d$groupaff)
d$groupaff <- factor(d$groupaff, levels = c(levels(d$groupaff)[3],levels(d$groupaff)[1],levels(d$groupaff)[2]))

#Disaster risk
d$disrisk_lower <- d$disrisk_pe  - 1.96*d$disrisk_se
d$disrisk_upper <- d$disrisk_pe  + 1.96*d$disrisk_se

d$groupdisrisk <- as.factor(d$groupdisrisk)
d$groupdisrisk <- factor(d$groupdisrisk, levels = c(levels(d$groupdisrisk)[3],levels(d$groupdisrisk)[2],levels(d$groupdisrisk)[1]))

#Risk acceptance
d$riskacc_lower <- d$riskacc_pe  - 1.96*d$riskacc_se
d$riskacc_upper <- d$riskacc_pe  + 1.96*d$riskacc_se

d$groupriskacc <- as.factor(d$groupriskacc)
d$groupriskacc <- factor(d$groupriskacc, levels = c(levels(d$groupriskacc)[2],levels(d$groupriskacc)[3],levels(d$groupriskacc)[1]))

#Patience
d$patience_lower <- d$patience_pe  - 1.96*d$patience_se
d$patience_upper <- d$patience_pe  + 1.96*d$patience_se

d$grouppatience <- as.factor(d$grouppatience)
d$grouppatience <- factor(d$grouppatience, levels = c(levels(d$grouppatience)[2],levels(d$grouppatience)[3],levels(d$grouppatience)[1]))

#Partisanship
d$party_lower <- d$party_pe  - 1.96*d$party_se
d$party_upper <- d$party_pe  + 1.96*d$party_se

d$groupparty <- as.factor(d$groupparty)
d$groupparty <- factor(d$groupparty, levels = c(levels(d$groupparty)[3],levels(d$groupparty)[1],levels(d$groupparty)[2]))

#Add labels
labs <- c(expression(paste(Delta, " Costs: Low")), "Costs: Low ($2 Million)", expression(paste(Delta, " Costs: Medium")), 
          "Costs: Medium ($8 Million)", "Costs: High ($14 Million)") 

#Create figures
figA2_env <- ggplot(d, aes(x=order, y=env_pe, colour=groupenv)) + 
  geom_point(aes(x = order, y = env_pe, colour = groupenv), show.legend = T, size = 2) +
  geom_segment(aes(x = order, xend = order, y = env_lower, yend = env_upper), show.legend = T, size=1) +
  coord_flip(ylim = c(-8, 10)) + theme_bw() +
  scale_x_continuous(name="", breaks=seq(1,5,1),labels=labs) +
  scale_y_continuous(name="Change in Support for Flood Control Dam (in points)",
                     breaks = c(-8,-6,-4,-2,0,2,4,6,8,10)) +
  geom_hline(yintercept = 0,size=.5,colour="black",linetype="dashed") +
  theme(legend.position = c(.85, 0.91),
        legend.background = element_rect(fill="transparent"),
        legend.key = element_blank(), axis.text = element_text(size = 16), axis.title = element_text(size = 16)) +
  scale_colour_grey(name = "") 
ggsave("FigureA2a_environmentalism.pdf", width=10, height=5)

figA2_aff <- ggplot(d, aes(x=order, y=aff_pe, colour=groupaff)) + 
  geom_point(aes(x = order, y = aff_pe, colour = groupaff), show.legend = T, size = 2) +
  geom_segment(aes(x = order, xend = order, y = aff_lower, yend = aff_upper), show.legend = T, size=1) +
  coord_flip(ylim = c(-6, 10)) + theme_bw() +
  scale_x_continuous(name="", breaks=seq(1,5,1),labels=labs) +
  scale_y_continuous(name="Change in Support for Flood Control Dam (in points)",
                     breaks = c(-6,-4,-2,0,2,4,6,8,10)) +
  geom_hline(yintercept = 0,size=.5,colour="black",linetype="dashed") +
  theme(legend.position = c(.85, 0.91),
        legend.background = element_rect(fill="transparent"),
        legend.key = element_blank(), axis.text = element_text(size = 16), axis.title = element_text(size = 16)) +
  scale_colour_grey(name = "") 
ggsave("FigureA2b_personalaffectedness.pdf", width=10, height=5)

figA2_disrisk <- ggplot(d, aes(x=order, y=disrisk_pe, colour=groupdisrisk)) + 
  geom_point(aes(x = order, y = disrisk_pe, colour = groupdisrisk), show.legend = T, size = 2) +
  geom_segment(aes(x = order, xend = order, y = disrisk_lower, yend = disrisk_upper), show.legend = T, size=1) +
  coord_flip(ylim = c(-6, 10)) + theme_bw() +
  scale_x_continuous(name="", breaks=seq(1,5,1),labels=labs) +
  scale_y_continuous(name="Change in Support for Flood Control Dam (in points)",
                     breaks = c(-6,-4,-2,0,2,4,6,8,10)) +
  geom_hline(yintercept = 0,size=.5,colour="black",linetype="dashed") +
  theme(legend.position = c(.85, 0.91),
        legend.background = element_rect(fill="transparent"),
        legend.key = element_blank(), axis.text = element_text(size = 16), axis.title = element_text(size = 16)) +
  scale_colour_grey(name = "") 
ggsave("FigureA2c_disasterriskbeliefs.pdf", width=10, height=5)

figA2_riskacc <- ggplot(d, aes(x=order, y=riskacc_pe, colour=groupriskacc)) + 
  geom_point(aes(x = order, y = riskacc_pe, colour = groupriskacc), show.legend = T, size = 2) +
  geom_segment(aes(x = order, xend = order, y = riskacc_lower, yend = riskacc_upper), show.legend = T, size=1) +
  coord_flip(ylim = c(-6, 10)) + theme_bw() +
  scale_x_continuous(name="", breaks=seq(1,5,1),labels=labs) +
  scale_y_continuous(name="Change in Support for Flood Control Dam (in points)",
                     breaks = c(-6,-4,-2,0,2,4,6,8,10)) +
  geom_hline(yintercept = 0,size=.5,colour="black",linetype="dashed") +
  theme(legend.position = c(.85, 0.91),
        legend.background = element_rect(fill="transparent"),
        legend.key = element_blank(), axis.text = element_text(size = 16), axis.title = element_text(size = 16)) +
  scale_colour_grey(name = "") 
ggsave("FigureA2d_riskaversion.pdf", width=10, height=5)

figA2_patience <- ggplot(d, aes(x=order, y=patience_pe, colour=grouppatience)) + 
  geom_point(aes(x = order, y = patience_pe, colour = grouppatience), show.legend = T, size = 2) +
  geom_segment(aes(x = order, xend = order, y = patience_lower, yend = patience_upper), show.legend = T, size=1) +
  coord_flip(ylim = c(-6, 10)) + theme_bw() +
  scale_x_continuous(name="", breaks=seq(1,5,1),labels=labs) +
  scale_y_continuous(name="Change in Support for Flood Control Dam (in points)",
                     breaks = c(-6,-4,-2,0,2,4,6,8,10)) +
  geom_hline(yintercept = 0,size=.5,colour="black",linetype="dashed") +
  theme(legend.position = c(.85, 0.91),
        legend.background = element_rect(fill="transparent"),
        legend.key = element_blank(), axis.text = element_text(size = 16), axis.title = element_text(size = 16)) +
  scale_colour_grey(name = "") 
ggsave("FigureA2e_patience.pdf", width=10, height=5)

figA2_party <- ggplot(d, aes(x=order, y=party_pe, colour=groupparty)) + 
  geom_point(aes(x = order, y = party_pe, colour = groupparty), show.legend = T, size = 2) +
  geom_segment(aes(x = order, xend = order, y = party_lower, yend = party_upper), show.legend = T, size=1) +
  coord_flip(ylim = c(-8, 10)) + theme_bw() +
  scale_x_continuous(name="", breaks=seq(1,5,1),labels=labs) +
  scale_y_continuous(name="Change in Support for Flood Control Dam (in points)",
                     breaks = c(-8,-6,-4,-2,0,2,4,6,8,10)) +
  geom_hline(yintercept = 0,size=.5,colour="black",linetype="dashed") +
  theme(legend.position = c(.85, 0.91),
        legend.background = element_rect(fill="transparent"),
        legend.key = element_blank(), axis.text = element_text(size = 16), axis.title = element_text(size = 16)) +
  scale_colour_grey(name = "") 
ggsave("FigureA2f_partisanidentification.pdf", width=10, height=5)









